package br.panthro.utils.xml
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import br.panthro.utils.loading.IAssetLoader;
	
	
	public class XMLLoader extends Sprite implements IAssetLoader
	{
		
		private var _loader:URLLoader;
		private var _xmlContent:XML;
		
		public function XMLLoader()
		{
			super();
		}
		
		public function load(url:String):void
		{
			_loader = new URLLoader();
			_loader.addEventListener( Event.COMPLETE, loadComplete );
			_loader.addEventListener( IOErrorEvent.IO_ERROR, loadError );
			_loader.addEventListener( IOErrorEvent.NETWORK_ERROR, loadError );
						
			_loader.load(new URLRequest(url));
		}
		
		public function loadComplete(event:Event):void
		{
			_xmlContent = new XML(event.target.data);
			dispatchEvent(new XMLEvent(XMLEvent.LOAD_COMPLETE));		
		}
		public function loadError(event:IOErrorEvent):void
		{
			dispatchEvent(new XMLEvent(XMLEvent.LOAD_ERROR));
		}
		
		public function get content():XML
		{
			return _xmlContent;
		}
	}
}